﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace h1
{
    public partial class Form_setMibao : Form
    {
        public bool confirm {  get; set; }
        string userID="unknown";
        public Form_setMibao()
        {
            InitializeComponent();
            confirm = false;
        }
        public Form_setMibao(string id)
        {
            InitializeComponent();
            confirm = false;
            userID = id;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                errorProvider1.SetError(textBox1, "请输入密保问题");
            }
            else if (textBox2.Text.Length == 0)
            {
                errorProvider1.SetError(textBox2, "请输入密保答案");
            }
            else if (textBox3.Text.Length == 0)
            {
                errorProvider1.SetError(textBox3, "请输入密保提示");
            }
            else
            {
                errorProvider1.Clear();
                Form_confirm fc = new Form_confirm(this, $"密保问题为：{textBox1.Text}\n密保答案为：{textBox2.Text}\n密保提示为：{textBox3.Text}", "是否确认并设置密保信息？");
                fc.ShowDialog();
                if (confirm)
                {
                    try
                    {
                        DataSet ds_users = MySqlHelper.getDataSet(MySqlHelper.Conn, "SELECT * FROM users");
                        DataRow[] rows = ds_users.Tables[0].Select($"userID='{userID}'");
                        if (rows.Count() < 1)
                        {
                            Form_message fm1 = new Form_message("没有找到该用户", $"userID：{userID}\ncount_rows：{rows.Count()}");
                            fm1.ShowDialog();
                        }
                        rows[0].BeginEdit();
                        rows[0]["userQuestion"] = textBox1.Text;
                        rows[0]["userAnswere"] = textBox2.Text;
                        rows[0]["userNote"] = textBox3.Text;
                        rows[0].EndEdit();
                        MySqlHelper.updateDataSet(MySqlHelper.Conn, "SELECT * FROM users", ds_users);
                        Form_message fm = new Form_message("密保问题设置成功！", $"密保问题为：{textBox1.Text}\n密保答案为：{textBox2.Text}\n密保提示为：{textBox3.Text}");
                        fm.ShowDialog();
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        Form_message fm = new Form_message("密保设置出错！", "错误描述：" + ex.Message);
                        fm.ShowDialog();
                    }
                }
            }
        }
    }
}
